package com.alex.controller.Topic_Idea_Assistant;

import cn.hutool.http.HttpUtil;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.alex.common.beans.ResultBean;
import com.alex.common.config.IndexConfig;
import com.alex.entity.ArticleEntity;
import com.alex.paramVo.ArticleSearchVo;
import com.alex.service.AllQueryBasicService;
import com.alex.service.FilterService;
import com.alex.utils.RedisUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RequestMapping("/topic_idea_assistant")
@RestController
public class Topic_Idea_Assistant_Controller extends AllQueryBasicService {
    public static void main(String[] args) {
        Map<String, Object> paramMap = new LinkedHashMap<>();
        paramMap.put("q", "hello");
        paramMap.put("from", "en");
        paramMap.put("to", "zh-CHS");
        //119.188.182.151:18081
        String body = HttpUtil.post("https://fanyi.yuntsg.com/v2/translate_a", paramMap);
        System.out.println(body);
    }
    @Resource
    private FilterService filterService;

    /**
     * 首页关系图
     *
     * @param uuid
     * @return
     * @throws SQLException
     * @throws IOException
     */
    @PostMapping("topic_idea_assistant")
    public ResultBean<Object> topic_idea_assistant(String uuid) throws SQLException, IOException {

        ArticleSearchVo searchVo = (ArticleSearchVo) RedisUtil.getObject(uuid);
        BoolQuery.Builder boolQuery = filterService.getBoolQuery(searchVo);
        Query query = boolQuery.build()._toQuery();
        List<String> list = Stream.of("aid").collect(Collectors.toList());
        List<Hit<ArticleEntity>> hits = allQuery(IndexConfig.CORE_INDEX, query, 0, 1000, list, ArticleEntity.class);
        List<ArticleEntity> coventList = coventList(hits);
        List<Integer> ids = coventList.stream().map(ArticleEntity::getAid).collect(Collectors.toList());
        System.out.println("课题思路助手的首页关系图的 ids___" + ids.size());
        ResultVoEntity resultVo = GeneNetWork.avg(ids);
        return new ResultBean<>(resultVo);
    }


}
